// pages/driver/car_list/car_list.js
var util = require('../../../utils/util.js');
Page({
  data:{
    IDD: '',
    list: [],
    loadError: false,
    noMoreData: false,
    dateEnd:'',
  },
  locateDriver: function (e) {
    var that = this
    var open = e.currentTarget.dataset.open
    console.log(that.data.dateEnd)
    var time1 = util.formatTime1(new Date())
    console.log(time1)
    if (that.data.dateEnd < time1) {
      that.showError('订单已过期')
      return
    }
    if (open == '0') {
      that.showError('此导游尚未开启定位功能')
      return
    }
    var driverID = e.currentTarget.id
    wx.navigateTo({
      url: '../../travelAgency/locate?IDD=' + that.data.IDD + '&DriverID=' + driverID + '&retype=4',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },






  onLoad:function(options){
      // 页面初始化 options为页面跳转所带来的参数
      var IDD1 = options.IDD;
      this.setData({
          IDD: IDD1,
          dateEnd: options.endtime,
      });

      getServiceData(this,1);
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  bindRetry:function(){
    getServiceData(this,1);
  },

  //下拉加载
  onPullDownRefresh: function(){
      getServiceData(this,1);

      //下拉动作完成之后，将index置为1，没有更多数据字段变为false
      this.data.pageIndex = 1;
      this.data.noMoreData = false;
  },
  callPhone:function(e){
    if (e.currentTarget.id == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
      wx.makePhoneCall({
          phoneNumber: e.currentTarget.id
      });
  },

  goMap:function(e){
      var that = this;
      var id = e.currentTarget.id;
      var item = this.data.list[id];
      if(item.GLclOpen == 0){
          wx.showModal({
              content: '对方没有打开位置共享功能！',
              showCancel:false,
              success: function(res) {
                  if (res.confirm) {
                    
                  }
              }
          });
      }else if(item.GLclOpen == 1){
        //   wx.getLocation({
        //         type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        //         success: function(res) {
        //             var latitude = res.latitude
        //             var longitude = res.longitude
        //             wx.openLocation({
        //                 latitude: latitude,
        //                 longitude: longitude,
        //                 name: 
        //                 scale: 28
        //             })
        //         }
        //   })
            wx.showToast({
                title: '正在加载...',
                icon: 'loading',
                duration: 10000
            });
            wx.request({
              url: 'https://www.lanshuicar.com/Server/CarCompany/CarCompMap.ashx',
              data: {
                  IDD: that.data.IDD,
                  id: item.GuiderID,
                  rtype: 3,
                  retype: 4
              },
              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              // header: {}, // 设置请求的 header
              success: function(res){
                  // success
                  var code = res.data.code;
                  console.log(res);
                  if(code == 0){
                        wx.openLocation({
                            latitude: parseFloat(res.data.data[0].mapW),
                            longitude: parseFloat(res.data.data[0].mapJ),
                            name: res.data.data[0].Address,
                            scale: 28
                        });
                  }else{
                      wx.showModal({
                          content: res.data.msg,
                          showCancel:false,
                          success: function(res) {
                              if (res.confirm) {

                              }
                          }
                      });
                  }
              },
              fail: function() {
                // fail
              },
              complete: function() {
                // complete
                wx.hideToast();
              }
            })
      }
  }
})

function getServiceData(that,pageIndex){
    var userType = wx.getStorageSync('userType');
    wx.showToast({
        title: '正在加载...',
        icon: 'loading',
        duration: 10000
    });

    if(pageIndex == 1){
        that.setData({
            list: []
        });
    }

    var userInfo = wx.getStorageSync('userInfo');
    wx.request({
      url: 'https://www.lanshuicar.com/Server/GetCarsGuidersCusts.ashx',
      data: {
          IDD: that.data.IDD,
          UID: userInfo.Id,
          UType: userType,
          OType: 2,
          PageSize: 20,
          PageIndex: pageIndex
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
          // success
          console.log(res);
          var code = res.data.code;
          if(code == 0){
              if(res.data.data.length > 0){
                  that.setData({
                      list: that.data.list.concat(res.data.data),
                      noMoreData: false
                  });
              }else{
                  that.setData({
                      noMoreData: true
                  });
              }
          }else{
              that.setData({
                  loadError: true
              });
          }
      },
      fail: function() {
          // fail
          that.setData({
              loadError: true
          });
      },
      complete: function() {
          // complete
          wx.hideToast();
          wx.stopPullDownRefresh();
      }
    })
}